package com.xysd.express.activity;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.bean.CompanyDetailsObj;
import com.xysd.express.util.GlideImageLoader;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.ScreenUtils;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

@ContentView(R.layout.activity_company_details)
public class CompanyDetailsActivity extends BaseActivity {

    @ViewInject(R.id.banner)
    private Banner banner;
    @ViewInject(R.id.company_img)
    private ImageView company_img;
    @ViewInject(R.id.company_tv)
    private TextView company_tv;
    @ViewInject(R.id.joinTime_tv)
    private TextView joinTime_tv;
    @ViewInject(R.id.tel_tv)
    private TextView tel_tv;
    @ViewInject(R.id.adds_tv)
    private TextView adds_tv;
    @ViewInject(R.id.introduce_tv)
    private TextView introduce_tv;

    private int id;
    private CompanyDetailsObj.CompanyDetails pageData;

    @Override
    protected void init() {
        id = getIntent().getIntExtra("id", 0);
        setTitleBar("企业详情");
        ViewGroup.LayoutParams params = banner.getLayoutParams();
        params.width = ScreenUtils.getScreenWidth(mActivity);
        params.height = ScreenUtils.getScreenWidth(mActivity) * 126 / 250;
        banner.setLayoutParams(params);
        getData();
    }

    private void getData() {
        HttpUtil.getCompanyDetails(id, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("企业详情", result);
                String code = JSONParser.getStringFromJsonString("Status", result);
                String msg = JSONParser.getStringFromJsonString("Msg", result);
                if ("0".equals(code)) {
                    CompanyDetailsObj obj = new Gson().fromJson(result, CompanyDetailsObj.class);
                    pageData = obj.Data;
                    setView(pageData);
                } else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("企业详情", error);
            }
        });
    }

    private void setView(CompanyDetailsObj.CompanyDetails pageData) {
        initBanner(pageData.ImageArray);
        Log.i("banner宽高", banner.getWidth() + ":" + banner.getHeight());
        Glide.with(mActivity).load(pageData.CompanyLogo).into(company_img);
        company_tv.setText(pageData.CompanyName);
        if (pageData.IsAuthentic) {
            Drawable drawable = getResources().getDrawable(R.mipmap.renzheng);
/// 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            company_tv.setCompoundDrawables(null, null, drawable, null);
            company_tv.setCompoundDrawablePadding(12);
        }
        joinTime_tv.setText(pageData.CreateDate);
        tel_tv.setText(pageData.Tel);
        adds_tv.setText(pageData.Address);
        introduce_tv.setText(pageData.Description);
    }


    private void initBanner(List<String> imgUrls) {
        //https://github.com/youth5201314/banner 教程地址
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imgUrls);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
//        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }
}
